﻿using System.Collections.Generic;
using System.Linq;

namespace Sidvall.Data
{
    public class RelationCollection<TRelation> : System.Collections.ObjectModel.Collection<TRelation>
    {
        #region Public Members

        #region AddRange

        public void AddRange(IEnumerable<TRelation> relations)
        {
            if (relations == null)
                return;
            foreach (var relation in relations)
                Add(relation);
        }

        #endregion
        #region RemoveAllExcept

        public void RemoveAllExcept(IEnumerable<TRelation> relations)
        {
            var list = (from o in relations
                        select o.ToString());
            RemoveAllExcept(list);
        }
        public void RemoveAllExcept(IEnumerable<string> relationNames)
        {
            if ((relationNames != null) && (relationNames.Any()))
            {
                var index = new HashSet<string>(relationNames, System.StringComparer.OrdinalIgnoreCase);
                var iMax = Count;
                for (int i = iMax - 1; i >= 0; i--)
                {
                    var item = this[i].ToString();
                    if (!index.Contains(item))
                        RemoveAt(i);
                }
            }
            else
            {
                Clear();
                return;
            }
        }

        #endregion

        #endregion
        #region Constructors

        public RelationCollection()
        {
        }

        #endregion
    }
}
